package com.cn.mobileproject.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.cn.mobileproject.R;
import com.cn.mobileproject.bean.PubInfo;
import com.cn.mobileproject.data.GlobalValue;
import com.cn.mobileproject.manager.PubinfoManager;
import com.cn.mobileproject.task.GenericTask;
import com.cn.mobileproject.task.TaskAdapter;
import com.cn.mobileproject.task.TaskListener;
import com.cn.mobileproject.task.TaskParams;
import com.cn.mobileproject.task.TaskResult;
import com.cn.mobileproject.utils.TextHelper;
import com.net.http.HttpException;

public class PubInfoActivity extends HeaderActivity{

	
	private static final String TAG = null;
	protected PubInfo pubInfo;
	protected TextView contentText;
	protected TextView creatorText;
	protected TextView pubDateText;
	protected TextView titleText;
	protected GenericTask loadTask;
	
	@Override
	protected void initComponent() {
		super.initComponent();
		contentText = (TextView)this.findViewById(R.id.pubinfo_text_content);
		creatorText = (TextView)this.findViewById(R.id.pubinfo_text_creator);
		pubDateText = (TextView)this.findViewById(R.id.pubinfo_text_pubdate);
		titleText = (TextView) this.findViewById(R.id.pubinfo_text_title);
		mRightBtn.setVisibility(View.GONE);
		
	}

	@Override
	protected void initComponentValue() {
		super.initComponentValue();
		setText(pubInfo);
	}
	
	public void setText(PubInfo pubInfo){
		if(null!=pubInfo){
			contentText.setText(TextHelper.getFliteStr(pubInfo.content));
			creatorText.setText(TextHelper.getFliteStr(pubInfo.creator));
			pubDateText.setText(TextHelper.getFliteStr(pubInfo.pubDate));
			mTitleText.setText(TextHelper.getFliteStr(pubInfo.typeValue));
			titleText.setText(TextHelper.getFliteStr(pubInfo.title));
		}
	}

	@Override
	protected void initEvent() {
		super.initEvent();
	}
	
	protected void onStart(){
		super.onStart();	
	}

	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pubinfo_detail);
		pubInfo = (PubInfo) getIntent().getSerializableExtra("pubInfo");
		initComponent();
		initEvent();
		initComponentValue();
		loadPubInfo();
	}
	
	public void loadPubInfo() {
		if (loadTask != null && loadTask.getStatus() == GenericTask.Status.RUNNING) {
			return;
		} else {
			loadTask = new DownLoadTask();
			loadTask.setListener(mInitTaskListener);
			TaskParams params = new TaskParams();
			loadTask.execute(params);
		}
	}
	
	public class DownLoadTask extends GenericTask {		
		@Override
		public TaskResult _doInBackground(TaskParams... params) {
			try {
				if(pubInfo!=null){
					pubInfo = PubinfoManager.getPubInfo(pubInfo.id, pubInfo.type);
					GlobalValue.getInstance().setPubInfoContent(pubInfo);
				}
			} catch (HttpException e) {
				e.printStackTrace();
				return TaskResult.FAILED;
			}
			return TaskResult.OK;
		}
	}
	
	public TaskListener mInitTaskListener = new TaskAdapter() {
		@Override
		public void onPostExecute(GenericTask task,TaskResult result){
			if (TaskResult.OK == result){
				setText(pubInfo);
			}
//			mListView.onRefreshComplete();
			if (loadTask != null && loadTask.getStatus() == GenericTask.Status.RUNNING) {
				loadTask.cancel(true);
			} 
		}
	};
	
}
